<template>
    <a-table ref="table" :scroll="{ y: tableHeight }" :columns="columns" :rowKey="(row, index) => row.Id" :dataSource="data"
        :bordered="true" size="small">

        <span slot="action" slot-scope="text, record">
            <template>
                <template>
                    <a-button type="link"> <a :href="record.PdfUrl" target="_blank">导出PDF</a> </a-button>
                    <a-divider type="vertical" />
                    <a-button type="link" @click="getWord(record)"> 导出Word </a-button>
                    <a-divider type="vertical" />
                    <a-button type="link" @click="getExcel(record)"> 导出Excel </a-button>
                </template>
            </template>
        </span>

    </a-table>
</template>
  
<script>
import {download} from "@/utils/util.js";
function createAdownload(href) {
    var a = document.createElement("a");
    a.href = href;
    a.style.display = "none";
    document.body.appendChild(a);
    a.click();
    a.remove();
}
const columns = [
    { title: '项目名称', dataIndex: 'ProjectName', width: '16%' },
    { title: '任务名称', dataIndex: 'TaskName', width: '16%' },
    { title: '报告名称', dataIndex: 'ReportName', width: '19%' },
    { title: '创建人', dataIndex: 'CreatorRealName', width: '15%' },
    {
        title: '创建日期', dataIndex: 'CreateTime', width: '10%', customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
        }
    },
    { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, width: '20%' }
]

export default {
    components: {

    },
    props: {
        pId: { require: true },
    },
    mounted() {
        this.$nextTick(() => {
            // 页面渲染完成后的回调
            this.tableHeight = this.getTableHeight(true) + 24
        })
        this.getDataList()
    },
    data() {
        return {
            tableHeight: 570, // 表格高度
            data: [],
            pagination: {
                current: 1,
                pageSize: 25,
                showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
                pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
                showSizeChanger: true
            },
            filters: {},
            entityId: '',
            sorter: { field: 'CreateTime', order: 'desc' },
            loading: false,
            columns,
            queryParam: { Condition: 'Name' },
            visible: false,
            imgUrl: require('@/assets/logo.png')
        }
    },
    methods: {
        handleTableChange(pagination, filters, sorter) {
            this.pagination = { ...pagination }
            this.filters = { ...filters }

            this.getDataList()
        },
        getDataList(TaskId) {
            this.loading = true
            this.$http
                .post("/Base_ProjectWork/Base_Project/GetProjectReportList", {
                    PageIndex: this.pagination.current,
                    PageRows: this.pagination.pageSize,
                    SortField: this.sorter.field || 'Id',
                    SortType: this.sorter.order,
                    Search: { ProjectId: this.pId, TaskId },
                })
                .then((resJson) => {
                    this.loading = false;
                    if (resJson.Success) {
                        this.data = resJson.Data
                    }
                    console.log(resJson);
                });
        },
        getWord(item) {
            if (item.WordUrl) {
                createAdownload(item.WordUrl);
                return;
            }
            item.loading = true;
            this.$http
                .post("/Base_ProjectWork/Base_Project/GetPdfToWord?Id=" + item.Id)
                .then((res) => {
                    item.loading = false;
                    if (res.Success) {
                        item.WordUrl = res.Data;
                        createAdownload(item.WordUrl);
                    } else this.$message.error(res.Msg);
                });
        },
        async getExcel(item) {
            console.log(download);
            item.loading = true;
            await download(
                this.$rootUrl +
                "/Base_ProjectWork/Base_Project/GetProjectReport?Id=" +
                item.Id,
                item.ReportName
            );
            item.loading = false;
            // this.$http.post().then((res) => {
            //   item.loading = false;
            //   if (res.Success) {
            //     createAdownload(res.Data);
            //   } else this.$message.error(res.Msg);
            // });
        },
    }
}
</script>
  